<template>
  <div class="m-login-container">
    <el-row class="m-main-header-search" justify="space-between">
      <el-form hide-required-asterisk :inline="true" ref="refForm">
        <el-row>
          <el-form-item prop="name" label="主体名称">
            <el-input
              clearable
              size="small"
              autofocus
              v-model="refFormData.name"
              placeholder="主体名称"
            ></el-input>
          </el-form-item>
          <el-form-item prop="status" label="状态">
            <el-select
              v-model="refFormData.status"
              size="small"
              filterable
              reserve-keyword
              placeholder="请选择状态"
            >
              <el-option
                v-for="item in statusList"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              />
            </el-select>
          </el-form-item>
<el-form-item>
          <el-button @click="handleSearch()" type="primary" size="small">
            查询
          </el-button>
          </el-form-item>
        </el-row>
      </el-form>
      <el-button
          @click="handleOpareteClick(1)"
          type="primary"
          size="small"
          icon="el-icon-circle-plus-outline"
        >
        新增
      </el-button>
    </el-row>
    <div class="m-main-content-body">
      <p class="contant-body-title">主体</p>
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="list"
        tooltip-effect="dark"
        style="width: 100%"
        stripe
        :header-cell-style="{
          'background-color': '#E5EEFF',
        }"
      >
        <el-table-column
          prop="name"
          label="主体名称"
          :autoFit="true"
          :fitHeader="true"
        >
        </el-table-column>
        <el-table-column
          prop="domain"
          label="域名"
          :autoFit="true"
          :fitHeader="true"
        ></el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          :autoFit="true"
          :fitHeader="true"
        >
          <template slot-scope="scope">
            {{ scope.row.status | getStatusName }}
          </template>
        </el-table-column>
        <el-table-column
          prop="time"
          label="时间"
          :autoFit="true"
          :fitHeader="true"
        >
          <template slot-scope="scope">
            {{
              scope.row.time
                ? $moment(scope.row.time).format("YYYY/MM/DD HH:mm:ss")
                : "--"
            }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          :autoFit="true"
          :fitHeader="true"
        >
          <template slot-scope="scope">
            <el-button
              class="table-operate-btn"
              @click="handleOpareteClick(2, scope.row)"
              type="text"
              size="small"
            >
              编辑
            </el-button>
            <el-button
              class="table-operate-btn red-btn-text"
              @click="del(scope.row.id)"
              type="text"
              size="small"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <dialog-set
      v-if="dislogSetVisible"
      @close="dislogSetVisible = false"
      :info="tempInfo"
      @getList="getList"
    ></dialog-set>
  </div>
</template>

<script>
import DialogSet from "./components/DialogSet";
import { statusList, getStatusName } from "./config/index";

export default {
  components: {
    DialogSet,
  },
  filters: {
    getStatusName,
  },
  data() {
    return {
      list: [],
      statusList,
      loading: false,
      dislogSetVisible: false,
      tempInfo: {},
      refFormData: {
        status: 1,
        name: "",
      },
      pageMeta: {
        page: 1,
        limit: 10,
        total: 1e10,
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    /*
     * @Desc: 查询
     * @params: {String}
     * @return: {Object}
     */
    async handleSearch() {
      this.pageMeta.page = 1;
      this.getList();
    },

    /*
     * @Desc: 获取列表
     * @params: {String}
     * @return: {Object}
     */
    async getList() {
      this.loading = true;
      const { data } = await this.$api.domain.getList({
        // ...this.pageMeta,
        // page: this.pageMeta.page - 1,
        ...this.refFormData,
      });
      this.__globalHandleResponseMixin(data, () => {
        this.list = data.data;
      });
      this.loading = false;
    },

    /*
     * @Desc: 编辑
     * @params: {String}
     * @return: {Object}
     */
    async handleOpareteClick(type, row = {}) {
      this.tempInfo = {
        type,
        ...row,
      };
      this.dislogSetVisible = true;
    },

    /*
     * @Desc: 删除
     * @params: {Number} id
     */
    async del(id) {
      this.$delConfirm(null, null, async () => {
        const { data } = await this.$api.domain.domainOperate({
          delete: {
            id,
          },
        });
        const { code } = data;
        if (code === 200) {
          this.$message.success("删除成功");
          this.handleSearch();
        }
      });
    },
  },
};
</script>

<style lang="less" scoped></style>
